package com.qh.basic.controller;


import com.qh.basic.exception.BusinessException;
import com.qh.basic.service.IVerifyCodeService;
import com.qh.basic.util.AjaxResult;
import com.qh.dto.PhoneCodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService IverifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){
        try {
            String base64Code = IverifyCodeService.getImageVerifyCode(key);
            return new AjaxResult(true,"操作成功",base64Code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto dto){
        try {
            IverifyCodeService.getPhoneCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto dto){
        try {
            IverifyCodeService.getBinderSmsCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

}
